package com.liaozibo.swagger.demo.controller;

import com.liaozibo.swagger.demo.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.concurrent.ConcurrentHashMap;

/**
 * author: liaozibo
 * createTime: 2021/3/14 22:38
 */
@RestController
@RequestMapping("/users")
@Api(tags = "用户相关接口")
public class UserController {

    private ConcurrentHashMap<Integer, User> users = new ConcurrentHashMap<>();

    @ApiOperation("新增用户接口")
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public void addUser(@RequestBody User user) {
        users.put(user.getId(), user);
    }

    @GetMapping("{id}")
    public User getUser(@PathVariable int id) {
        return users.get(id);
    }

    @PutMapping
    public void updateUser(@RequestBody User user) {
        if (users.contains(user.getId())) {
            users.put(user.getId(), user);
        }
    }

    @ApiIgnore // 在文档隐蔽该接口
    @DeleteMapping("{/id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void deleteUser(@PathVariable int id) {
        users.remove(id);
    }
}
